//
//  CZOrginal.swift
//  Weibo24
//
//  Created by apple on 16/3/1.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SnapKit

/*
    原创微博 -- 
    头像
    认证图片
    微博名字
    会员等级
    发表时间
    发表来源
    发表内容
*/

let CellMargin = 10

class CZOrginal: UIView {

    
    // 定义一个约束,用于记录底部
    var bottomConstarint: Constraint?
    
    
   
    //定义一个属性
    var statusViewModel: CZStatusViewModel?{
        
        didSet{
            
            nameLabel.text = statusViewModel?.statusModel?.user?.screen_name
            
//            contentLabel.text = statusViewModel?.statusModel?.text
        
            if let text = statusViewModel?.statusModel?.text{
            
                //显示原创微博的图文混排
                contentLabel.attributedText = statusViewModel?.getAttributeString(text)
            }
            
            
            
            sourceLabel.text = statusViewModel?.dealSource(statusViewModel?.statusModel?.source)
            
            //显示时间
            timeLabel.text = statusViewModel?.dealCreateDate(statusViewModel?.statusModel?.created_at)
            
            veriftyImageView.image = statusViewModel?.verifity_image
            
            //设置等级
            levelImageView.image = statusViewModel?.level_image
            
            /*
            显示不显示配图视图
            当有配图的时候,展示配图,修改约束
            没有的时候,就隐藏,修改约束
            */
            // 配图视图数组有配图,并且count > 0
            
            //清空约束
            bottomConstarint?.uninstall()
            
            //判断原创微博的配图数组是否满足条件
            // 第一/数组有数据, 第二个 数组的数据 > 0
            if let pic_urls = statusViewModel?.statusModel?.pic_urls where pic_urls.count > 0{
            
//                print(statusViewModel?.statusModel?.text)
                
                pictureView.hidden = false
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //底部应该等于配图视图的底部
                   bottomConstarint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                
                //传值
//                pictureView.pictureViewModel = statusViewModel
                
                //需要把 model转换成ViewModel
                
                //定义一个临时数组进行数据拼接
                var result = [CZPictureViewModel]()
                // 通过遍历原创微博的配图视图数组,来把 CZPictureModel 传递给 CZPictureViewModel
                for model in pic_urls{
                
                    let viewModel  = CZPictureViewModel()
                    
                    viewModel.model = model
                    
                    result.append(viewModel)
                }
                 // [CZPictureViewModel]
                pictureView.pictureList = result
                
            } else {
                
                pictureView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //没有配图视图,底部应该在 contentLabel的底部
                   bottomConstarint = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
            }
        }
        
    }
    
   // 三部曲
    //重写构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    //
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    // 添加子控件和约束
    private func setupUI(){
    
        addSubview(photoImageView)
        addSubview(veriftyImageView)
        addSubview(nameLabel)
        addSubview(levelImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        
        //添加配图视图
        addSubview(pictureView)
        
        //头像约束 -- 左-10 上边 10 宽 40 高 40
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(CellMargin)
            make.top.equalTo(self.snp_top).offset(CellMargin)
            
            make.size.equalTo(CGSizeMake(40, 40))
        }
        //认证图片的约束 -- 中心点
        veriftyImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(photoImageView.snp_right)
            make.centerY.equalTo(photoImageView.snp_bottom)
        }
        
        // 名字 --左边 上边
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(CellMargin)
            make.top.equalTo(photoImageView.snp_top)
        }
        
        //等级 -- 左边 中心线
        levelImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(CellMargin)
            make.centerY.equalTo(nameLabel.snp_centerY)
        }
        
        // 时间的约束 -- 左 10,上边 5
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(CellMargin)
            make.top.equalTo(nameLabel.snp_bottom).offset(CellMargin/2)
            
        }
        
        // 来源 -- 左边 10 ,中心线
        sourceLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(timeLabel.snp_right).offset(CellMargin)
            make.centerY.equalTo(timeLabel.snp_centerY)
            
        }
        
        // 设置内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self.snp_left).offset(CellMargin)
            
            make.top.equalTo(photoImageView.snp_bottom).offset(CellMargin)
//            make.right.equalTo(self.snp_right).offset(10)
            //给contentLabel固定了宽度
            make.width.equalTo(kUIScreenWidth - 2*CGFloat(CellMargin))
            //
//            make.bottom.equalTo(self.snp_bottom)
            
        }
        
        
        /*
            按照添加转发微博的思路添加
            1.默认都有配图视图
            2.默认就由一张图片 -- 宽高是固定死的
        */
        // 配图视图的约束
        /*
            显示不显示配图视图
            当有配图的时候,展示配图,修改约束
            没有的时候,就隐藏,修改约束
        */
        /*
            
        */
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(CellMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(CellMargin)
            //肯定不可能在这里更新约束
//            make.size.equalTo(CGSizeMake(100, 100))
            
        }
        
        //告诉视图的底部是哪里
        self.snp_makeConstraints { (make) -> Void in
           bottomConstarint = make.bottom.equalTo(pictureView.snp_bottom).offset(CellMargin).constraint
        }
        
    }
    
    //MARK:--懒加载
///         头像
   private lazy var photoImageView: UIImageView = {
    
    let img = UIImageView()
    
    img.image = UIImage(named: "avatar_default_big")
    
    return img
    }()
///      认证图片
    private lazy var veriftyImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_vip")
        
        return img
    }()
///      微博名字
    private lazy var nameLabel: UILabel = {
    
        let label = UILabel()
        
        label.text = "微博名字"
        
        label.font = UIFont.systemFontOfSize(15)
        
        
        return label
        
    }()
///      会员等级
    private lazy var levelImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "common_icon_membership_level1")
        
        return img
    }()
    
///      发表时间
    private lazy var timeLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "时间"
        
        label.font = UIFont.systemFontOfSize(13)
        
        label.textColor = UIColor.orangeColor()
        
        return label
        
    }()
///      发表来源
    private lazy var sourceLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "微博来源"
        
        label.font = UIFont.systemFontOfSize(13)
        
        
        return label
        
    }()
///      发表内容
    private lazy var contentLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "微博内容"
        
        label.font = UIFont.systemFontOfSize(15)
        
        label.numberOfLines = 0
        
        return label
        
    }()
    
    private lazy var pictureView: CZPictureView = CZPictureView()
    
}
